package top.heliming.youyou.cart.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.heliming.youyou.cart.client.GoodsClient;
import top.heliming.youyou.cart.interceptor.LoginInterceptor;
import top.heliming.youyou.cart.pojo.Cart;
import top.heliming.youyou.cart.service.CartService;
import top.heliming.youyou.common.utils.JsonUtils;
import top.heliming.youyou.entity.UserInfo;
import top.heliming.youyou.item.pojo.Sku;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/06/14 下午 10:25
 */
@Service
public class CartServiceImpl implements CartService {
  @Autowired
  private StringRedisTemplate redisTemplate;

  @Autowired
  private GoodsClient goodsClient;

  static final String KEY_PREFIX = "ly:cart:uid:";

  static final Logger logger = LoggerFactory.getLogger(CartService.class);

  @Override
  public void addCart(Cart cart) {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();
    // Redis的key
    String key = KEY_PREFIX + user.getId();
    // 获取hash操作对象
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    // 查询是否存在
    Long skuId = cart.getSkuId();
    Integer num = cart.getNum();
    Boolean boo = hashOps.hasKey(skuId.toString());
    if (boo) {
      // 存在，获取购物车数据
      String json = hashOps.get(skuId.toString()).toString();
      cart = JsonUtils.toBean(json, Cart.class);
      // 修改购物车数量
      cart.setNum(cart.getNum() + num);
    } else {
      // 不存在，新增购物车数据
      cart.setUserId(user.getId());
      // 其它商品信息， 需要查询商品服务
      Sku sku = this.goodsClient.querySkuById(skuId).getBody();
      cart.setImage(StringUtils.isBlank(sku.getImages()) ? "" : StringUtils
          .split(sku.getImages(), ",")[0]);
      cart.setPrice(sku.getPrice());
      cart.setTitle(sku.getTitle());
      cart.setOwnSpec(sku.getOwnSpec());
    }
    // 将购物车数据写入redis
    hashOps.put(cart.getSkuId().toString(), JsonUtils.toString(cart));
  }

  @Override
  public List<Cart> queryCartList() {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();

    // 判断是否存在购物车
    String key = KEY_PREFIX + user.getId();
    if(!this.redisTemplate.hasKey(key)){
      // 不存在，直接返回
      return null;
    }
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    List<Object> carts = hashOps.values();
    // 判断是否有数据
    if(CollectionUtils.isEmpty(carts)){
      return null;
    }
    // 查询购物车数据
    return carts.stream().map(o -> JsonUtils.toBean(o.toString(), Cart.class)).collect(Collectors.toList());
  }

  @Override
  public void updateNum(Long skuId, Integer num) {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();
    String key = KEY_PREFIX + user.getId();
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    // 获取购物车
    String json = hashOps.get(skuId.toString()).toString();
    Cart cart = JsonUtils.toBean(json, Cart.class);
    cart.setNum(num);
    // 写入购物车
    hashOps.put(skuId.toString(), JsonUtils.toString(cart));
  }

  @Override
  public void deleteCart(String skuId) {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();
    String key = KEY_PREFIX + user.getId();
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    hashOps.delete(skuId);
  }
}
